package com.canbot.u05.activity.customvoice;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.activity.customvoice.adapter.CustomAudioAdapter;
import com.canbot.u05.activity.customvoice.bean.CustomAudioData;
import com.canbot.u05.activity.customvoice.tool.GridSpacingItemDecoration;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

/**
 * Created by liu.heng  on 2018/5/24.
 * 自定义对话选择音频
 */
public class ChooseAudioActivity extends BaseActivityNoTitle implements CustomAudioAdapter.OnItemClickListener {

        private static final String TAG = ChooseAudioActivity.class.getName();

        private RecyclerView mRecyclerView;

        private RelativeLayout mRelativeLayout;

        private RelativeLayout remindRelativeLayout;

        private CustomAudioAdapter mCustomImageAdapter;

        private List<CustomAudioData> mCustomAudioData;

        private Dialog dialogLoading;

        private String voice;

        private String audioPath;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_choose_audio);
                final Intent intent = getIntent();
//                selectAudio = intent.getStringExtra("soundPath");
                voice = getIntent().getStringExtra("voice");
                audioPath = getIntent().getStringExtra("path");

                initView();
        }

        private void initView() {
                mRelativeLayout = (RelativeLayout) findViewById(R.id.my_relativeLayout);
                remindRelativeLayout = (RelativeLayout) findViewById(R.id.remind_relativeLayout);

                mRecyclerView = (RecyclerView) findViewById(R.id.my_gridView);
                mRecyclerView.setLayoutManager(new GridLayoutManager(ChooseAudioActivity.this, 4));  //网格布局，每行为3
                mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(4, 120, false));
                getData();

                mCustomImageAdapter = new CustomAudioAdapter(this, mCustomAudioData, this);

                mRecyclerView.setAdapter(mCustomImageAdapter);

        }

        @Override
        public void onItemClick(View view) {
                final int i = (int) view.getTag();
                mCustomImageAdapter.setChecked(i);
                mCustomImageAdapter.notifyDataSetChanged();
                voice = mCustomAudioData.get(i).getPath();
                final boolean isSuccess = MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, voice);
                Logger.e(TAG, "播放路径==" + voice);
                if (!isSuccess) {
                        ToastTools.newInstance(getApplicationContext()).showText(getString(R.string.network_error));
                }
        }

        private void getData() {
                mCustomAudioData = new ArrayList<>();

                //http://192.168.12.103:7755/getAudioList
                dialogLoading = LoadingDialogUtils.createLoadingDialog(ChooseAudioActivity.this, getResources().getString(R.string.request));
                dialogLoading.show();
                final String url = "http://" + IpMsgConst.SOCKET_HOST + ":7755/getAudioList";
                final HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("path", audioPath);
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                Log.e(TAG, result);
                                final JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                Logger.e(TAG, "onSuccess:" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "strMsgBean.getRc()" + strMsgBean.getRc());
                                if (strMsgBean.getRc() == 0) {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        dialogLoading.setCancelable(true);
                                                        dialogLoading.dismiss();
                                                        final List<CustomAudioData> list = JSON.parseArray(strMsgBean.getResult().toString(), CustomAudioData.class);

                                                        Collections.sort(list, new Comparator<CustomAudioData>() {
                                                                @Override
                                                                public int compare(CustomAudioData audio, CustomAudioData t1) {
                                                                        return audio.getName().compareTo(t1.getName());
                                                                }
                                                        });
                                                        if (list.size() > 0) {
                                                                mCustomAudioData.addAll(list);
                                                                if (!TextUtils.isEmpty(voice)) {
                                                                        for (int i = 0; i < mCustomAudioData.size(); i++) {
                                                                                if (voice.equals(mCustomAudioData.get(i).getPath())) {
                                                                                        mCustomImageAdapter.setChecked(i);
                                                                                        break;
                                                                                }
                                                                        }
                                                                }
                                                                mCustomImageAdapter.notifyDataSetChanged();
                                                        }
                                                        else {
                                                                remindRelativeLayout.setVisibility(View.VISIBLE);
                                                        }

                                                }
                                        });
                                }
                        }

                        @Override
                        public void onFailure() {
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                dialogLoading.dismiss();
                                                ToastTools.newInstance(getApplicationContext()).showText(ChooseAudioActivity.this.getString(R.string.network_error));
                                        }
                                });
                        }
                });
        }

        /*确认*/
        public void nextStep(View view) {
                if (TextUtils.isEmpty(voice)) {
                        ToastTools.newInstance(getApplicationContext()).showText(ChooseAudioActivity.this.getString(R.string.please_select_audio));
                        return;
                }
                final Intent intent = new Intent();
                intent.putExtra("path", voice);
                intent.putExtra("isChecked", true);
                setResult(Activity.RESULT_OK, intent);
                finish();
        }

        /*返回*/
        public void back(View view) {
                setResult(RESULT_CANCELED);
                finish();
        }
}
